/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pclong.util;

import java.util.Locale;
import javax.servlet.ServletContextEvent;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.WebApplicationContextUtils;

/**
 *
 * 获取spring bean对象
 * 2011-09-28
 * @author long@7road.com
 */
public class BeanUtil extends ContextLoaderListener {

    /**
     * spring上下文环境   
     */
    private static ApplicationContext applicationContext;
    private static final Log logger = LogFactory.getLog(BeanUtil.class);

    @Override
    public void contextInitialized(ServletContextEvent event) {
        super.contextInitialized(event);
        applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(event.getServletContext());
        logger.info("BeanUtil finish init");
//        Locale local = new Locale("zh_CN");
//        logger.info("Login.title"+applicationContext.getMessage("login.title", null, "", local));
//        BeanUtil.GetLocalMessage(CONTEXT_CLASS_PARAM, args, CONTEXT_CLASS_PARAM, local)
    }

    public static Object getBean(String beanName) {
        return applicationContext.getBean(beanName);
    }

    public static String GetLocalMessage(String code, Object[] args, String defaultMessage, Locale locale) throws NoSuchMethodException {
        return applicationContext.getMessage(code, args, defaultMessage, locale);
    }

    public static String GetLocalMessage(String code, Object[] args, Locale locale) throws NoSuchMethodException {
        return GetLocalMessage(code, args, "", locale);
    }
}
